package gosee

import (
	"html/template"
)

type Template struct {
	Templates *template.Template
	Config    *config
	FuncMap   template.FuncMap
}

type config struct {
	staticPath     string // 静态文件路径
	templatePath   string // 模板文件路径
	templateSuffix string // 模板文件后缀
}

func NewTemplate() *Template {
	return &Template{
		FuncMap: make(template.FuncMap),
		Config: &config{
			staticPath:     "static",
			templatePath:   "template/",
			templateSuffix: ".tmpl",
		},
	}
}

func (t *Template) AddFunc(name string, fn interface{}) {
	if t.FuncMap == nil {
		t.FuncMap = make(template.FuncMap)
	}
	t.FuncMap[name] = fn
}

func (t *Template) Load(pattern string) error {
	var err error
	t.Templates, err = template.New("").Funcs(t.FuncMap).ParseGlob(pattern)
	return err
}

func (t *Template) SetConfig(c *config) {
	t.Config = c
}

func (t *Template) SetStaticPath(addr string) {
	t.Config.staticPath = addr
}

func (t *Template) SetTemplatePath(addr string) {
	t.Config.staticPath = addr
}

func (t *Template) SetTemplateSuffix(suffix string) {
	t.Config.staticPath = suffix
}

func (t *Template) GetStaticPath() string {
	return t.Config.staticPath
}

func (t *Template) GetTemplatePath() string {
	return t.Config.templatePath
}

func (t *Template) GetTemplateSuffix() string {
	return t.Config.templateSuffix
}
